package studyguide.ch19;

import java.io.IOException;

import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class MobileMediaMidlet extends MIDlet {

	private final Command playCommand;
	private final Command exitCommand;
	private final Display display;

	private final List contents;
	private final MediaDetailsForm playForm;
	private final MediaRecordForm recordForm;
	
	private class SampleCommandListener implements CommandListener {
		public void commandAction(Command command, Displayable displayable) {
			if (command == exitCommand) {
				destroyApp(true);
				notifyDestroyed();
			} else if (command == playCommand) {
				String uri = contents.getString(contents.getSelectedIndex());
				if (uri.startsWith("capture://")) {
					recordForm.initialize(uri);
					display.setCurrent(recordForm);
				} else {
					playForm.initialize(uri);
					display.setCurrent(playForm);
				}
			} else {
				throw new RuntimeException("UNKNOWN COMMAND" + command);
			}
		}
	}
	
	public MobileMediaMidlet() throws IOException {
		playCommand = new Command("Play", Command.OK, 1);
		exitCommand = new Command("Exit", Command.EXIT, 1);
		
		contents = new List("MMAPI", Choice.IMPLICIT);
		playForm = new MediaDetailsForm(this);
		recordForm = new MediaRecordForm(this);
		
		contents.setSelectCommand(playCommand);
		contents.addCommand(exitCommand);
		
		SampleCommandListener listener = new SampleCommandListener();
		contents.setCommandListener(listener);
		display = Display.getDisplay(this);
		
		// Display multimedia information about device:
		System.out.println("microedition.media.version:" + System.getProperty("microedition.media.version"));
		System.out.println("supports.mixing:" + System.getProperty("supports.mixing"));
		System.out.println("supports.audio.capture:" + System.getProperty("supports.audio.capture"));
		System.out.println("supports.video.capture:" + System.getProperty("supports.video.capture"));
		System.out.println("supports.recording:" + System.getProperty("supports.recording"));
		System.out.println("audio.encodings:" + System.getProperty("audio.encodings"));
		System.out.println("video.encodings:" + System.getProperty("video.encodings"));
		System.out.println("video.snapshot.encodings:" + System.getProperty("video.snapshot.encodings"));
		System.out.println("streamable.contents:" + System.getProperty("streamable.contents"));
		
		// Assemble list:
		contents.append("http://localhost:8080/JavaMEStudyGuide-Web/video.mpg", null);
		contents.append("http://localhost:8080/JavaMEStudyGuide-Web/music.mid", null);
		contents.append("capture://audio?encoding=pcm&rate=8000&bits=8&channels=1", null);
		contents.append("capture://video?encoding=rgb565&width=160&height=120", null);
	}
	
	protected void destroyApp(boolean unconditional) {
	}

	protected void pauseApp() {
	}

	protected void startApp() throws MIDletStateChangeException {
		showMainScreen();
	}

	public void showMainScreen() {
		display.setCurrent(contents);	
	}
	
}
